package main

import (
	"encoding/json"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
)

func tabbaRename() {
	assetsMap := make(map[string]*ImageSet)
	filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
		if info.IsDir() {
			return nil
		}

		setname := ""
		theme := ""
		lowerPath := strings.ToLower(path)
		if strings.Contains(lowerPath, "account") {
			setname = "tab_account_" + theme
		} else if strings.Contains(lowerPath, "categ") {
			setname = "tab_categories_" + theme
		} else if strings.Contains(lowerPath, "home") {
			setname = "tab_home_" + theme
		} else if strings.Contains(lowerPath, "cart") {
			setname = "tab_bag_" + theme
		} else if strings.Contains(lowerPath, "superbrand") {
			setname = "tab_explore_" + theme
		} else {
			return nil
		}

		if strings.Contains(lowerPath, "pressed") {
			setname += "chosen_25x31"
		} else {
			setname += "unchosen_25x31"
		}

		suffix := ".png"
		scale := "1x"
		if strings.Contains(path, "50x62") {
			suffix = "@2x.png"
			scale = "2x"
		} else if strings.Contains(path, "@3x") {
			suffix = "@3x.png"
			scale = "2x"
		}

		fileName := setname + suffix
		imgset, ok := assetsMap[setname]
		if !ok {
			imgset = NewImageSet()
			assetsMap[setname] = imgset
		}
		imgItem := NewImageItem()
		imgItem.Filename = fileName
		imgItem.Scale = scale
		imgset.AddImageItem(imgItem)

		dirName := setname + ".imageset"
		os.Mkdir(dirName, 0755)
		os.Rename(path, dirName+"/"+fileName)

		return nil
	})
	for key, item := range assetsMap {
		contentPath := key + ".imageset/Contents.json"
		buff, err := json.Marshal(item)
		if err == nil {
			ioutil.WriteFile(contentPath, buff, 0755)
		}
	}
}
